<template>
  <el-form :model="formData" label-width="auto" style="max-width: 600px">
    <el-form-item label="用户名">
      <el-input v-model="formData.username" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="formData.password" type="password" placeholder="请输入密码" disabled />
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="formData.email" placeholder="请输入邮箱" />
    </el-form-item>
    <el-form-item label="用户性别">
      <el-select v-model="formData.sex" placeholder="选择性别">
        <el-option label="男" value="男" />
        <el-option label="女" value="女" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button align="center" type="primary" @click="onSubmit">提交</el-button>
      <el-button align="center" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { defineProps, defineEmits, onMounted,watch } from "vue";

import { ref } from "vue";
import requestUtil from "@/api/request.js";
import { ElMessage } from "element-plus";

// do not use same name with ref
const props = defineProps({
  formData: {
    type: Object,
    required: true
  },
  userId: {
    type: Number,
    required: true
  }
});
let formData = ref({})
let userId = ref(0)
const getUserInfo = async () => {
  console.log(props.userId);
  let res = await requestUtil.get("management/getUserInfo?userId=" + userId.value);
  formData.value = res.data.userInfo;
}
// formData.value = props.formData 
const getUserId= () => {
  console.log(props.userId);
  userId.value = props.userId;
  console.log(userId);
  getUserInfo();
}
getUserId();

const emits = defineEmits(["cancel", "submit"]);
const cancel = () => {
  emits("cancel");
};
const onSubmit = async () => {

  let res = await requestUtil.put("management/updateUserInfo/",{userId: props.userId, userInfo: formData.value});
  console.log(res);
  if (res.data.code === 200) {
    ElMessage.success("修改成功");
    emits("submit");
  }
  
};
</script>